package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.common.exception.BusinessException;
import com.demo.pojo.dto.RoomListDTO;
import com.demo.pojo.entity.Room;
import com.demo.service.RoomService;
import com.demo.mapper.RoomMapper;
import io.micrometer.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author lenovo
* @description 针对表【room】的数据库操作Service实现
* @createDate 2025-01-27
*/
@Service
@Slf4j
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room>
    implements RoomService{

    private final RoomMapper roomMapper;

    public RoomServiceImpl(RoomMapper roomMapper) {
        this.roomMapper = roomMapper;
    }

    @Override
    public Room selectById(Integer id) {
        //根据教室id查询教室信息
        Room room = roomMapper.selectById(id);
        //如果没查到抛出异常
        if (room == null){
            throw new BusinessException("没有此教室信息");
        }
        log.info("查询教室信息成功:{}", room);
        return room;
    }

    @Override
    public void saveRoom(Room room) {
        // 验证必填字段
        if (room.getRoName() == null || room.getRoName().trim().isEmpty()) {
            throw new BusinessException("教室名称不能为空");
        }
        if (room.getRoCapacity() == null) {
            throw new BusinessException("教室容量不能为空");
        }
        if (room.getRoCampusId() == null) {
            throw new BusinessException("校区ID不能为空");
        }
        
        //新增前判断教室名称有无重复
        if (roomMapper.selectByName(room.getRoName()) != null){
            throw new BusinessException("教室名称重复");
        }
        
        // 设置创建时间
        room.setRoUpdateDate(new Date());
        
        //执行新增操作
        int result = roomMapper.insert(room);
        if (result <= 0) {
            throw new BusinessException("新增教室失败");
        }
        log.info("新增教室信息成功:{}", room);
    }

    @Override
    public void updateRoom(Room room) {
        //修改前判断是否存在该教室
        Room oldRoom = roomMapper.selectById(room.getRoId());
        if (oldRoom == null){
            throw new BusinessException("没有此教室信息");
        }
        //修改前判断教室名称有无变动
        //如果查询出的教室名称和修改前的教室名称一致，则不进行修改
        if (!oldRoom.getRoName().equals(room.getRoName())){
           //教室名称不一致，说明教室名称被修改，查询有无重复
            if (roomMapper.selectByName(room.getRoName()) != null){
                throw new BusinessException("教室名称重复");
            }
        }
        //设置修改时间
        room.setRoUpdateDate(new Date());
        //执行修改操作
        roomMapper.updateById(room);
        log.info("修改教室信息成功:{}", room);
    }

    @Override
    public Map<String, Object> listPage(RoomListDTO dto) {
        //1. 创建分页对象
        Page<Room> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        // 2. 构建查询条件
        LambdaQueryWrapper<Room> lambdaQueryWrapper = new LambdaQueryWrapper<Room>()
                .like(StringUtils.isNotBlank(dto.getRoName()),Room::getRoName,dto.getRoName())
                .eq(dto.getRoCapacity() != null,Room::getRoCapacity,dto.getRoCapacity())
                .eq(dto.getRoCampusId() != null,Room::getRoCampusId,dto.getRoCampusId());

        // 3. 执行分页查询
        Page<Room> pageResult = roomMapper.selectPage(page, lambdaQueryWrapper);

        log.info("分页查询结果：{}", pageResult);
        // 4. 返回结果
        return Map.of(
                "records", pageResult.getRecords(),
                "total", pageResult.getTotal(),
                "current", pageResult.getCurrent()
        );
    }

    @Override
    public List<RoomListDTO> getRoomList() {
        // 查询所有教室
        List<Room> rooms = list();
        List<RoomListDTO> roomListDTOs = new ArrayList<>();
        
        for (Room room : rooms) {
            RoomListDTO dto = new RoomListDTO();
            dto.setRoId(room.getRoId());
            dto.setRoName(room.getRoName());
            dto.setRoCapacity(room.getRoCapacity());
            dto.setRoCampusId(room.getRoCampusId());
            dto.setRoRemark(room.getRoRemark());
            dto.setRoUpdateDate(room.getRoUpdateDate());
            roomListDTOs.add(dto);
        }
        
        return roomListDTOs;
    }

}



